// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database();
// 云函数入口函数
exports.main = async (event, context) => {
  let card_id = event.card_id;
  let user_name = event.user_name;
  let user_address = event.user_address;
  let user_openid = event.user_openid;
  let user_phone_number = event.user_phone_number;
  let goods_id = event.goods_id;
  let goods_number = event.goods_number;
  let status = event.status;
  let user_delivery_date = event.user_delivery_date;
  let update_time = new Date();
  // 更改提货卡状态
  db.collection('ops_card').where({
    _id: card_id
  }).update({
    data: { 
      status: 3,   // 已提货
      update_time: update_time
    },
  })
  // 新增订单
  return await db.collection("ops_order").add({
    data: {
      user_name: user_name,
      user_address: user_address,
      user_openid: user_openid,
      user_phone_number: user_phone_number,
      goods_id: goods_id,
      goods_number: goods_number,
      status: status,
      user_delivery_date: user_delivery_date,
      update_time: update_time,
      card_id: card_id
    },
    success: function (res) {
      console.log(res)
    },
    fail: console.error,
    complete: console.log
  });
}